#include "Surface.h"

Surface::Surface(LPDIRECT3DDEVICE9 pd3dDevice)
{
	_pd3dDevice = pd3dDevice;
}

Surface::~Surface()
{
	if (_image != NULL) _image->Release();
}

bool Surface::LoadSurface(LPCSTR filename, D3DCOLOR transcolor)
{
	LPDIRECT3DSURFACE9 surface = NULL;
	HRESULT result;

	// get width and height from file
	result = D3DXGetImageInfoFromFile(filename, &_info);

	if (result != D3D_OK)
		return NULL;

	// create surface
	result = _pd3dDevice->CreateOffscreenPlainSurface(_info.Width,
		_info.Height,
		D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT,
		&_image,
		NULL);

	if (result != D3D_OK)
		return false;

	//load surface from file into newly created surface
	result = D3DXLoadSurfaceFromFile(_image,
		NULL,
		NULL,
		filename,
		NULL,
		D3DX_DEFAULT,
		transcolor,
		NULL);

	// make sure file was load okay
	if (result != D3D_OK)
		return false;

	if (_image != NULL)
	{
		_width = _info.Width;
		_height = _info.Height;
	}
	else
	{
		_width = _height = -1;
	}

	return true;
}